<template>
  <el-dialog
      v-model="centerDialogVisible"
      :title="formTitle"
      width="20%"
      destroy-on-close
      center
      @close="dialogClose">
    <el-form
        ref="ruleFormRef"
        label-position="top"
        label-width="100px"
        :model="formLabelAlign"
        :rules="rules"
    >
      <el-form-item label="设备编号" prop="deviceNo">
        <el-input v-model="formLabelAlign.deviceNo"/>
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input v-model="formLabelAlign.deviceName"/>
      </el-form-item>
    </el-form>
    <div class="bottomButton">
      <el-button class="affirm" @click="centerDialogVisible = false" style="width: 47.5%;height: 100%;">取消</el-button>
      <el-button class="affirm" @click="resetForm" style="width: 47.5%;height: 100%;">确认</el-button>
    </div>
  </el-dialog>

</template>

<script>
let me;
export default {
  name: "AddDeviceDialog",
  data() {
    return {
      formTitle: '',
      rules: {
        deviceNo: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ],
        deviceType: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ],
        deviceName: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ]
      },
      formLabelAlign: {
        deviceNo: '',
        deviceType: 1,
        deviceName: ''
      },
      centerDialogVisible: false
    }
  },
  methods: {
    showDialog(row, parentId,deviceType) {
      me = this;
      me.formLabelAlign.deviceType=deviceType
      this.centerDialogVisible = true;
      this.$nextTick(() => {
        this.formTitle = '新增设备信息';
        me.formLabelAlign.actionType = 'deviceAdd';
        me.formLabelAlign.rangeId = parentId;
        if (row !== '' && row.parentId) {
          this.formTitle = '新增子集设备信息';
          me.formLabelAlign.actionType = 'deviceAdd';
          me.formLabelAlign.rangeId = parentId;
          me.formLabelAlign.parentId = row.parentId;
          me.formLabelAlign.deviceType = deviceType;
          me.typeDisabled = false;
        }else if(row !== '' ){
          me.formLabelAlign = JSON.parse(JSON.stringify(row));
          this.formTitle = '编辑设备信息';
          me.formLabelAlign.rangeId = row.rangeId;
          me.formLabelAlign.actionType = 'deviceUpdate';
          me.typeDisabled = false;
        }
      });
    },
    resetForm() {
      me.$refs.ruleFormRef.validate(async (valid, fields) => {
        if (valid) {
          let response = await me.$asyncPost("device", me.formLabelAlign);
          if (response.success) {
            me.$message.success('添加成功');
            me.centerDialogVisible = false;
            me.$parent.queryData(true);
          }
        } else {
          console.log('error submit!', fields)
        }
      })
    },
    dialogClose() {
      this.$refs.ruleFormRef.resetFields();//关闭的时候一定要清空之前的form表单已填写的数据！！！！！
    }
  }
}
</script>

<style scoped lang="scss">

</style>